// pages/login/login.js
const app = getApp({})
Page({

    /**
     * 页面的初始数据
     */
    data: {
        username:"",
        openid:"",
        userpassword:"",
        roleClass:['学生'],
        selectRole:"请选择身份",
        userInputPla:"请输入学号/工号",
        titleInfo:"高校智能云教室"
    },
    /* 跳转到注册页面 */
    NavToRegister(){
        wx.showToast({
          title: '暂未开启公开注册',
          icon:"none"
        })
    },
    /* 忘记密码 */
    forgetPassword(){
        wx.showModal({
          title: '提示',
          content: '请保证账号已经绑定实际手机号,否则请联系管理员'
        })
    },
    /* 获取账号 */
    userInput(e){
        console.log(e.detail.value);
        this.setData({
            username:e.detail.value
        })
    },
    /* 获取密码 */
    pwdInput(e){
        console.log(e.detail.value);
        this.setData({
            userpassword:e.detail.value
        })
    },
    /* 选择身份 */
    bindPickerChange(e){
        /* console.log(e); */
        var item = this.data.roleClass[e.detail.value]
        console.log(item);
        this.setData({
            selectRole:item
        })
        if(item!="学生"){
            this.setData({
                userInputPla:"请输入工号"
            })
        }else{
            this.setData({
                userInputPla:"请输入学号"
            })
        }
    },
    /* 获取openid */
    login(){
        console.log(1);
        if(this.data.username.length==0 || this.data.userpassword.length==0){
            wx.showToast({
              title: '填写完整',
              icon:"error"
            })
            return
        }
        if(this.data.selectRole=="请选择身份" || this.data.selectRole.length==0){
            wx.showToast({
                title: '请选择身份',
                icon:"error"
              })
              return
        }
        if(this.data.selectRole=="学生"){
            wx.showLoading({
              title: '登录中',
            })
            console.log(this.data.username,this.data.userpassword);
            wx.request({
                url:app.globalData.baseUrl+ '/admin/users/login',
                method: 'POST',
                data: {
                  studentId: this.data.username,
                  password: this.data.userpassword
                },
                header: {
                  'content-type': 'application/json'
                },
                success: function(res) {
                    wx.hideLoading()
                    console.log(res.data);
                    wx.setStorageSync('userInfo', res.data.data.user)
                    console.log("缓存",wx.getStorageSync('userInfo'));
                    wx.switchTab({
                        url: '/pages/index/index',
                    })
                },fail:(res)=>{
                    wx.hideLoading()
                    console.log(res.data);
                }
            })
        }
        
    },
    /* 学生登录 */
    studentloginConfirm(){
        wx.showLoading({
          title: '登录中',
        })
        wx.request({
          url: app.globalData.testUrl+'/user/login',
          method:"POST",
          data:{
              openid:this.data.openid,
              studentid:this.data.username,
              password:this.data.userpassword,
          },success:(res_2)=>{
                wx.hideLoading()
                /* console.log(res_2) */
                if(res_2.data.status==200){
                    this.saveStrogSysncStudent(this.data.username,"学生")
                }else{
                    wx.showToast({
                      title: res_2.data.statusMsg,
                      icon:"none"
                    })
                }
          },fail:(res_2)=>{
                wx.hideLoading()
                console.error(res_2)
          }
        })
    },
    /* 维修工登录 */
    fixLogin(){
        wx.showLoading({
          title: '登陆中',
        })
        wx.request({
            url: app.globalData.testUrl+'/fixwork/login',
            method:"POST",
            data:{
                phone:this.data.username,
                password:this.data.userpassword,
            },success:(res_2)=>{
                wx.hideLoading()
                console.log(res_2)
                if(res_2.data.status==200){
                    this.saveStrogSysncFix(this.data.username,"维修工",res_2.data.username,res_2.data.field,res_2.data.area)
                }else{
                    wx.showToast({
                      title: res_2.data.statusMsg,
                      icon:"none"
                    })
                }
            },fail:(res_2)=>{
                wx.hideLoading()
                console.error(res_2)
            }
        })
    },
    /* 管理员登录 */
    adminLogin(){
        wx.showLoading({
          title: '登录中',
        })
        wx.request({
            url:app.globalData.testUrl + '/admin/login',
            method:"POST",
            data:{
                phone:this.data.username,
                password:this.data.userpassword,
            },success:(res_2)=>{
                wx.hideLoading()
                console.log(res_2)
                if(res_2.data.status==200){
                    this.saveStrogSysnc(this.data.username,"管理员")
                }else{
                    wx.showToast({
                      title: res_2.data.statusMsg,
                      icon:"none"
                    })
                }
            },fail:(res_2)=>{
                wx.hideLoading()
                console.error(res_2)
            }
        })
    },
    saveStrogSysncStudent(userid,identity){
        var tempInfo={}
        tempInfo.userid=userid
        tempInfo.identity=identity
        console.log(tempInfo);
        wx.setStorageSync('userInfo', tempInfo)
        wx.switchTab({
          url: '/pages/index/index',
        })
    },
    saveStrogSysnc(userid,identity){
        var tempInfo={}
        tempInfo.userid=userid
        tempInfo.identity=identity
        console.log(tempInfo);
        wx.setStorageSync('userInfo', tempInfo)
        wx.switchTab({
          url: '/pages/worktop/worktop',
        })
    },
    saveStrogSysncFix(userid,identity,username,feild,area){
        console.log(userid,identity,username,feild,area);
        var obj = {
            userid:userid,
            identity:identity,
            username:username,
            feild:feild,
            area:area
        }
        wx.setStorageSync('userInfo', obj)
        wx.switchTab({
            url: '/pages/worktop/worktop',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(wx.getStorageSync('userInfo'));
        if(wx.getStorageSync('userInfo')){
            wx.showModal({
              title: '您已登录，无需重复登录',
              content: '点击确定进入主页',
              complete: (res) => {
                if (res.cancel) {
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                }
                if (res.confirm) {
                    wx.switchTab({
                        url: '/pages/index/index',
                    })
                }
              }
            })
        }
    },
})